package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmall.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.security.LoginPrincipal;
import cn.tedu.csmall.passport.service.impl.AdminServiceImpl;
import cn.tedu.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@Api(tags = "01.管理员管理控制器")
@RestController
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    AdminServiceImpl adminService;

    @ApiOperation(value = "管理员登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO){
        log.debug("开始处理管理员登录的请求,参数:{}",adminLoginDTO);
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation(value = "添加管理员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAnyAuthority('/ams/admin/add-new')")
    public JsonResult<Void> addNew(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                   AdminAddNewDTO adminAddNewDTO){
        adminService.AddNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation(value = "管理员列表")
    @ApiOperationSupport(order = 110)
    @GetMapping("")
    @PreAuthorize("hasAnyAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListItemVO>> list(
            @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理查看管理员列表的请求");
        log.debug("当前登录的当事人:{}", loginPrincipal);
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据id删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAnyAuthority('/ams/admin/delete')")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理删除管理员请求");
        adminService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id启用管理员")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAnyAuthority('/ams/admin/update')")
    public JsonResult<Void> setEnable(@PathVariable Long id){
        log.debug("开始处理启用该管理员请求");
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id禁用管理员")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/unEnable")
    @PreAuthorize("hasAnyAuthority('/ams/admin/update')")
    public JsonResult<Void> unEnable(@PathVariable Long id){
        log.debug("开始处理启用该管理员请求");
        adminService.unEnable(id);
        return JsonResult.ok();
    }
}
